<?php
/**
 * geoMatcher.php
 *
 * INPUT:       $_REQUEST['country'] = the country to compare with
 * OUTPUT:      an XML file of countries and their similarity %.
 *
 * First, rudimentary version of GeoMatcher.
 * Compares the popular tracks of the given country with the popular tracks of every other country.
 * Returns the percentage of "similarity" for every country in an XML file.
 *
 * TO DO:
 * - Quite slow, so maybe we can store the result on the server every day?
 * - Other, more sophisticated method of comparison?
 *
 * By Laurens Van Keer
 */
require 'auth.php';
require 'country_list.php';
ini_set(max_execution_time, 0);
set_time_limit(0);
ignore_user_abort(true);
       
header('Content-Type: text/xml');
header('Connection: keep-alive');

if ( empty($_REQUEST['country']) ) {
        echo '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>' . PHP_EOL;
        echo '<Error>No country specified!</Error>' . PHP_EOL;
} else {

        $country = $_REQUEST['country'];
        $data = '';
       
        if ( file_exists("matches/$country.xml") ) {
       
                $fh = fopen("matches/$country.xml", "r");
                $data = fread($fh, filesize("matches/$country.xml"));
                fclose($fh);
       
        } else {
                       
                $data .= '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>' . PHP_EOL;
                $data .= '<GeoMatches>' . PHP_EOL;
       
                $apiClass = new lastfmApi();
                $geoClass = $apiClass->getPackage($auth, 'geo', $config);
               
                $countryVars = array(
                        'country' => $_REQUEST['country']
                );
       
                if ( $countryTracks = $geoClass->getTopTracks($countryVars) && $countryArtists = $geoClass->getTopArtists($countryVars) ) {
               
                        // make array of track names
                        $trackNames = array();
                        for($i=0; $i<sizeof($countryTracks); $i++) {
                                $trackNames[$i] = $countryTracks[$i]['name'];
                        }
                       
                        // make array of artists names
                        $artistNames = array();
                        for($i=0; $i<sizeof($countryArtists); $i++) {
                                $artistNames[$i] = $countryArtists[$i]['name'];
                        }
                       
                        foreach ( $country_list as $match ) {
                                $matchVars = array('country' => $match);
                                if ( $matchTracks = $geoClass->getTopTracks($matchVars) && $matchArtists = $geoClass->getTopArtists($matchVars) ) {
                               
                                        // build xml
                                        $data .= '<GeoMatch>' . PHP_EOL;
                                                $data .= '<Country>' . $match . '</Country>' . PHP_EOL;
                                                $data .= '<Similarity>' . similarity($trackNames, $matchTracks, $artistNames, $matchArtists) . '</Similarity>' . PHP_EOL;
                                        $data .= '</GeoMatch>' . PHP_EOL;
                                       
                                }
                        }
                       
                } else {
                        $data .= '<Error>Error ' . $geoClass->error['code'] . ' - ' . $geoClass->error['desc'] . '</Error>' . PHP_EOL;
                }
               
                $data .= '</GeoMatches>' . PHP_EOL;
       
                // Write to file
                $fh = fopen("matches/$country.xml", "w");
                fwrite($fh, $data);
                fclose($fh);
               
        }
       
        // Print XML to screen
        echo $data;
}

/**
 * FUNCTIONS
 */
 
function similarity(&$originalTracks, &$matchTracks, &$originalArtists, &$matchArtists)
{
        if ( ($nbTracks = sizeof($matchTracks)) == 0 || ($nbArtists = sizeof($matchArtists)) == 0 )
                return 0;
               
        // tracks
        $trackMatches = 0;
        for($i=0; $i<$nbTracks; $i++){
                if(in_array($matchTracks[$i]['name'], $originalTracks))
                        ++$trackMatches;
        }
       
        //artists
        $artistMatches = 0;
        for($i=0; $i<$nbArtists; $i++){
                if(in_array($matchArtists[$i]['name'], $originalArtists))
                        ++$artistMatches;
        }
       
        return (($trackMatches + $artistMatches) / ($nbTracks + $nbArtists)) * 100;
}

set_time_limit(30);
ini_set(max_execution_time, 300);
?>